<?php
namespace app\admin\controller;

use think\Request;
use think\Db;
use app\admin\model\BannerModel;
class Banner extends Base {

    /**
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $BannerModel = new BannerModel();

            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('banner')
                    ->count();//计算总页面
            $order=input('banner_time');//排序方式
            $list = $BannerModel->getUsersByWhere($order,$page,$limit);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('banner/index');
    }

    /***
     * 删除
     */
    public function batchDelBanner(){
        $ids = explode(',',input('param.ids'));
        $BannerModel = new BannerModel();
        $flag = $BannerModel->batchDelBanner($ids);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data['detail_type'] = input('param.cate_id');
            $data['banner_title'] = input('param.title');
            $data['banner_img'] = input('param.images');
            if($data['detail_type']==1){
                $data['activities_id'] = input('param.type_name');
            }elseif ($data['detail_type']==2){
                $data['classroom_id'] = input('param.type_name');
                $classroom_price = Db::table('up_classroom')->where('id',input('param.type_name'))->find();
                $data['price'] = $classroom_price['classroom_price'];
                $data['type'] = $classroom_price['classroom_cate'];
            }elseif ($data['detail_type']==3){
                $data['banner_link'] = input('param.link');
            }elseif ($data['detail_type']==4){
                $data['mechanism_id'] = input('param.type_name');
            }elseif ($data['detail_type']==5){
                $data['infomation_id'] = input('param.type_name');
            }elseif ($data['detail_type']==6){
                 $data['banner_content'] = input('param.content');
            }
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_banner')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }else{
            $activities = Db::table('up_institutional_activities')->where('activity_sh',1)->select();
            $this->assign('list',$activities);
        }

        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 搜索类型
     */
    public function selectType(){
        $data = array();
        $type_val=input('param.type_val');
        if($type_val==1){
            $list = Db::table('up_institutional_activities')->where('activity_sh',1)->field('id,activity_name')->select();
            $data['code'] = 1;
            $data['data'] =$list;
            return json($data);
        }elseif ($type_val==2){
            $list = Db::table('up_classroom')->where('classroom_status',2)->field('id,classroom_title')->select();
            $data['code'] = 2;
            $data['data'] =$list;
            return json($data);

        }elseif ($type_val==4){
            $list = Db::table('up_mechanism')->where('mechanism_status',2)->field('mechanism_id,mechanism_name')->select();
            $data['code'] = 4;
            $data['data'] =$list;
            return json($data);

        }elseif ($type_val==5){
            $list = Db::table('up_information')->where('information_status',1)->field('information_id,information_title')->select();
            $data['code'] =5;
            $data['data'] =$list;
            return json($data);
        }
    }

    public function update(){
        $list = array();
        $banner_id = input('param.id');
        $res = Db::table('up_banner')->where('banner_id',$banner_id)->find();
        if(request()->isAjax()){
            $data['detail_type'] = input('param.cate_id');
            $data['banner_title'] = input('param.title');
            $data['banner_img'] = input('param.images');
            if($data['detail_type']==1){
                $data['activities_id'] = input('param.type_name');
            }elseif ($data['detail_type']==2){
                $data['classroom_id'] = input('param.type_name');
                $classroom_price = Db::table('up_classroom')->where('id',input('param.type_name'))->find();
                $data['price'] = $classroom_price['classroom_price'];
                $data['type'] = $classroom_price['classroom_cate'];
            }elseif ($data['detail_type']==3){
                $data['banner_link'] = input('param.link');
            }elseif ($data['detail_type']==4){
                $data['mechanism_id'] = input('param.type_name');
            }elseif ($data['detail_type']==5){
                $data['infomation_id'] = input('param.type_name');
            }elseif ($data['detail_type']==6){
                $data['banner_content'] = input('param.content');
            }
            $where['banner_id']= input('param.id');
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_banner')->where($where)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '修改失败']);
            }
        }
        if($res['detail_type']==1){
            $list = Db::table('up_institutional_activities')->where('activity_sh',1)->field('id,activity_name')->select();
            foreach ($list as $key=>&$vo){
                $vo['ids'] = $vo['id'];
                $vo['name'] = $vo['activity_name'];
            }
            $res['f_id'] = $res['activities_id'];

        }elseif ($res['detail_type']==2){
            $list = Db::table('up_classroom')->where('classroom_status',2)->field('id,classroom_title')->select();
            foreach ($list as $key=>&$vo){
                $vo['ids'] = $vo['id'];
                $vo['name'] = $vo['classroom_title'];
            }
            $res['f_id'] = $res['classroom_id'];
        }elseif ($res['detail_type']==4){
            $list = Db::table('up_mechanism')->where('mechanism_status',2)->field('mechanism_id,mechanism_name')->select();
            foreach ($list as $key=>&$vo){
                $vo['ids'] = $vo['mechanism_id'];
                $vo['name'] = $vo['mechanism_name'];
            }
            $res['f_id'] = $res['mechanism_id'];

        }elseif ($res['detail_type']==5){
            $list = Db::table('up_information')->where('information_status',1)->field('information_id,information_title')->select();
            foreach ($list as $key=>&$vo){
                $vo['ids'] = $vo['information_id'];
                $vo['name'] = $vo['information_title'];
            }
            $res['f_id'] = $res['infomation_id'];

        }
        $aa = array(3,6);
        $this->assign('res',$list);
        $this->assign('ads',$res);
        $this->assign('aa',$aa);
        return $this->fetch();
    }
}